package cn.broadsense.web.controller.biz;

import cn.broadsense.biz.dto.ChannelTagTreeDto;
import cn.broadsense.biz.service.ChannelTagService;
import cn.broadsense.biz.vo.ChannelTagVo;
import cn.broadsense.biz.vo.TagTypeVo;
import cn.broadsense.common.core.controller.BaseController;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.core.domain.entity.SysDictData;
import cn.broadsense.common.core.page.TableDataInfo;
import cn.broadsense.common.core.page.TableDataInfoResult;
import cn.broadsense.common.utils.SecurityUtils;
import cn.broadsense.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 点位数据
 *
 * @author: pengxin
 * @create: 2023-09-12 11:19
 * @Version 1.0
 **/
@RestController
@Api(tags = "点位标签数据")
@RequestMapping("/channel/tag")
public class ChannelTagController extends BaseController {


    @Resource
    private ChannelTagService channelTagService;

    @Autowired
    private ISysDictDataService dictDataService;


    @PreAuthorize("@ss.hasPermi('biz:channelTag:treeList')")
    @GetMapping("/treeList")
    @ApiOperation("获取点位标签树列表")
    public R<List<ChannelTagTreeDto>> treeTagList(@RequestParam(value = "isFilter", required = false) Boolean isFilter) {
        List<ChannelTagTreeDto> channelTagTreeDtos = channelTagService.tagTree(isFilter);
        return R.ok(channelTagTreeDtos);
    }

    @PreAuthorize("@ss.hasPermi('biz:channelTag:treeList')")
    @GetMapping("/mapTreeList")
    @ApiOperation("获取三维地图点位标签树列表")
    public R<List<ChannelTagTreeDto>> mapTreeTagList() {
        List<ChannelTagTreeDto> channelTagTreeDtos = channelTagService.mapTagTree();
        return R.ok(channelTagTreeDtos);
    }

    @PreAuthorize("@ss.hasPermi('biz:channelTag:treeList')")
    @GetMapping("/contentTreeList")
    @ApiOperation("标签内容树")
    public R<List<ChannelTagTreeDto>> contentTreeTagList() {
        List<ChannelTagTreeDto> channelTagTreeDtos = channelTagService.contentTreeTag();
        return R.ok(channelTagTreeDtos);
    }

    @PreAuthorize("@ss.hasPermi('biz:channelTag:add')")
    @PostMapping
    @ApiOperation("添加标签")
    public R addTag(@RequestBody @Validated ChannelTagVo channelTagVo) {
        channelTagService.insertTag(channelTagVo);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('biz:channelTag:remove')")
    @DeleteMapping("/{id}")
    @ApiOperation("删除标签")
    public R delete(@PathVariable Long id) {
        channelTagService.delete(id);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('biz:channelTag:edit')")
    @PutMapping("/{id}")
    @ApiOperation("更新标签")
    public R update(@PathVariable Long id, @RequestBody @Validated ChannelTagVo tagVo) {
        channelTagService.edit(id, tagVo);
        return R.ok();
    }

    @PostMapping("/relationChannel/{tagId}")
    @PreAuthorize("@ss.hasPermi('biz:channelTag:relation')")
    @ApiOperation("关联标签一对多")
    public R relationChannel(@PathVariable Long tagId, @RequestBody List<Long> channelIds) {
        channelTagService.relationChannel(tagId, channelIds);
        return R.ok();
    }

    @PostMapping("/relationChannelV2/{channelId}")
    @PreAuthorize("@ss.hasPermi('biz:channelTag:relation')")
    @ApiOperation("关联标签多对一")
    public R relationChannelV2(@PathVariable Long channelId, @RequestBody List<Long> tagIdIds) {
        channelTagService.relationChannelV2(channelId, tagIdIds);
        return R.ok();
    }

    @ApiOperation("标签类型列表")
    @GetMapping("/tagTypeList")
    public R<TableDataInfoResult<List<SysDictData>>> tagTypeList() {
        startPage();
        SysDictData dictData = new SysDictData();
        dictData.setDictType("tag_type");
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return R.ok(getDataTableResult(list));
    }

    @ApiOperation("添加标签类型")
    @PostMapping("/tagType")
    public R addTagType(@RequestBody @Validated TagTypeVo tagTypeVo) {
        SysDictData dictData = new SysDictData();
        dictData.setDictSort(0L);
        dictData.setDictType("tag_type");
        dictData.setDictLabel(tagTypeVo.getDictLabel());
        dictData.setDictValue(tagTypeVo.getDictValue());
        dictData.setStatus(tagTypeVo.getStatus());
        dictData.setListClass("default");
        dictData.setCreateBy(SecurityUtils.getUsername());
        dictData.setCreateTime(new Date());
        dictDataService.insertDictData(dictData);
        return R.ok();
    }

    @ApiOperation("修改标签类型")
    @PutMapping("/tagType/{dictCode}")
    public R addTagType(@PathVariable Long dictCode, @RequestBody @Validated TagTypeVo tagTypeVo) {
        SysDictData dictData = new SysDictData();
        dictData.setDictSort(0L);
        dictData.setDictCode(dictCode);
        dictData.setDictType("tag_type");
        dictData.setDictLabel(tagTypeVo.getDictLabel());
        dictData.setDictValue(tagTypeVo.getDictValue());
        dictData.setStatus(tagTypeVo.getStatus());
        dictData.setUpdateBy(SecurityUtils.getUsername());
        dictData.setUpdateTime(new Date());
        dictDataService.updateDictData(dictData);
        return R.ok();
    }

    @ApiOperation("删除标签类型")
    @DeleteMapping("/tagType/{dictCode}")
    public R deleteTagType(@PathVariable Long dictCode) {
        dictDataService.deleteDictDataById(dictCode);
        return R.ok();
    }

}
